var app = getApp()
var temp = []
var serviceId = "0000ffe0-0000-1000-8000-00805f9b34fb"
var characteristicId = "0000ffe1-0000-1000-8000-00805f9b34fb"
var searchCardResult = false
var selectCardResult = false
var index = 0
var cardInfoArray = new Array()
var util = require("../../utils/util.js");
var analyCardTimer
var strName = '';
var strSex = '';
var strNation = '';
var strBirth = null;
var strAddr = null;
var strIdCode = null;
var strIssue = null;
var strBeginDate = null;
var strEndDate = null;
let that;
var api = require('../../utils/request')
Page({
	data: {
		detectCode: '',
		idCardNo: '',
		idCardName: '',
		idCardSex: '',
		telephone: '',
		remark: '',
		userUid: '',
		connectedDeviceId: '',
		devices: [],
		isbluetoothready: false,
		deviceconnected_index: 0,
		searchingstatus: false,
		is_open_blue:false,//监听初始化页面时是否打开蓝牙
	},
	onLoad: function (options) {
		// if(!wx.getStorageSync('info')){
		// 	return;
		// }
		that = this;
		that.switchBlueTooth();
		// console.log("connectedDeviceId", getApp().globalData.connectedDeviceId);
		// let connectedDeviceId = getApp().globalData.connectedDeviceId;
		// this.setData({
		// 	connectedDeviceId: connectedDeviceId
		// })
		// if (connectedDeviceId) {
		// 	this.blueReceive()
		// }
	},
	scan() {
		wx.scanCode({
			onlyFromCamera: true,
			success(res) {
				that.setData({
					detectCode: res.result
				})
			}
		})
	},
	getPhone(e) {
		this.setData({
			telephone: e.detail.value
		})
	},
	getremarks(e) {
		this.setData({
			remark: e.detail.value
		})
	},
	submit() {
		if(!wx.getStorageSync('info')){
			wx.showToast({
			  title: '请登录账号！',
			  icon:"none"
			})
			return;
		}
		let {
			detectCode,
			idCardNo,
			idCardName,
			idCardSex,
			telephone,
			remark
		} = this.data;
		if (!detectCode) {
			wx.showToast({
				title: '检测码不能为空！',
				icon:'none'
			})
			return
		}
		if (!idCardNo) {
			wx.showToast({
				title: '身份证号码不能为空！',
				icon:'none'
			})
			return
		}
		if (!idCardName) {
			wx.showToast({
				title: '姓名不能为空！',
				icon:'none'
			})
			return
		}
		if (!idCardSex) {
			wx.showToast({
				title: '性别不能为空！',
				icon:'none'
			})
			return
		}
		api.post({
			apiCode: "IReaderManageService.uploadDetectData",
			params: {
				pigDetectRecEx: {
					detectCode: detectCode,
					detectTime: util.formatTime(new Date()),
					idCardName: idCardName,
					idCardNo: idCardNo,
					idCardSex: idCardSex,
					telephone: telephone,
					remarks: remark,
					userUid: wx.getStorageSync("info").userUid
				}
			}
		}).then(res => {
			if (res.code == 0) {
				wx.showToast({
					title: '保存成功！',
				})
				that.reset();
			} else {
				wx.showToast({
					icon: "none",
					title: res.msg,
				})
			}
		});
	},
	reset(){
		this.setData({
			detectCode: '',
			idCardNo :'',
			idCardName: "",
			idCardSex: "",
			telephone:'',
			remark:''
		})
	},
	switchBlueTooth: function () {
		wx.openBluetoothAdapter({
			success: function (res) {
				that.setData({
					is_open_blue:true
				})
				//蓝牙打开成功，若当前没有链接设备，搜索设备
				that.searchbluetooth();
				wx.onBluetoothAdapterStateChange(function (res) {
					console.log("监听蓝牙状态改变",res);
					if(!res.available){
						let devices = that.data.devices;
						let connect_index = that.data.deviceconnected_index;
						devices[connect_index].deviceconnected = false;
						that.setData({
							deviceconnected: false,
							devices:devices
						})						
					}
					that.setData({
						isbluetoothready: res.available,
						searchingstatus: res.discovering
					})
				})
				//监听蓝牙搜索到设备回调
				wx.onBluetoothDeviceFound(function (devices) {
					console.log("监听搜索回调",devices)
					if (!devices.devices[0].name) {
						// that.setData({
						// 	searchingstatus: false
						// })
						return
					} else {
						temp.push(devices.devices[0])
						that.setData({
							devices: temp
						});
						console.log('发现新蓝牙设备',devices);
						//本地连过的历史记录
						let local_devices = wx.getStorageSync("devices");
						if(local_devices && local_devices[0].deviceId ==  devices.devices[0].deviceId){
							that.connectTO(devices.devices[0]);
						}
						wx.stopBluetoothDevicesDiscovery({
							success: function (res) {
								console.log("停止蓝牙搜索")
							}
						})
					}
				})
				//接收数据
				wx.onBLECharacteristicValueChange(function (characteristic) {
					let buffer = characteristic.value
					let dataView = new DataView(buffer)
					var intArray = new Uint8Array(buffer) //将arraybuffer转换为array
					if (!searchCardResult && searchCardIsOk(intArray)) { //寻卡成功
						searchCardResult = true
						wx.writeBLECharacteristicValue({ //选卡
							deviceId: that.data.connectedDeviceId,
							serviceId: serviceId,
							characteristicId: characteristicId,
							value: util.stringToArrayBuffer('aaaaaa96690003200221'),
							success: function (res) {
								console.log('writeBLECharacteristicValue success 2', res.errMsg)
							},
						})
					} else if (searchCardResult && !selectCardResult) {
						if (selectCardIsOk(intArray)) { //选卡成功
							selectCardResult = true
							wx.writeBLECharacteristicValue({ //读卡
								deviceId: that.data.connectedDeviceId,
								serviceId: serviceId,
								characteristicId: characteristicId,
								//value: util.stringToArrayBuffer('aaaaaa96690003301023'),   //读文字+照片+指纹信息，小程序无法解析照片，故显示都无照片信息
								value: util.stringToArrayBuffer('aaaaaa96690003300132'), //读文字+照片信息，小程序无法解析照片，故显示都无照片信息
								success: function (res) {
									console.log('writeBLECharacteristicValue success 3', res)
								}
							})
						} else {
							startSearchCard(that); //寻卡
						}
					} else if (searchCardResult && selectCardResult) {
						for (var i = 0; i < intArray.byteLength; i++) {
							cardInfoArray[index] = intArray[i]
							index++ //二代证 1297 三代证2321
						}
						clearTimeout(analyCardTimer)
						//if ((cardInfoArray.length == 2321) || (cardInfoArray.length == 1297)){
						if ((cardInfoArray.length == 2321) || (cardInfoArray.length == 1295)) {
							analyCardTimer = setTimeout(function () {
								wx.hideLoading();
								readCardIsOk(cardInfoArray)
								updateInfo(that)
							}, 500)
						}

					} else {
						startSearchCard(that); //寻卡
					}

					var str = ""
					for (var i = 0; i < dataView.byteLength; i++) {
						// console.log((dataView.getUint8(i)))
						// str += String.fromCharCode(dataView.getUint8(i))
						var temp = (dataView.getUint8(i)).toString(16)
						var len = temp.length
						while (len < 2) {
							temp = '0' + temp //补位
							len++
						}
						// str = (dataView.getUint8(i)).toString(16)
						str += temp
					}
				})				
			},
			fail: function (res) {
				wx.showModal({
					title: '提示',
					content: '请检查手机蓝牙是否打开',
					success: function (res) {
						that.setData({
							is_open_blue:false,
							isbluetoothready: false,
							searchingstatus: false
						})
					}
				})
			}
		})
	},
	searchbluetooth: function () {
		if(!this.data.is_open_blue){
			//打开小程序前未打开蓝牙，点击搜索时需要重新初始化蓝牙
			this.switchBlueTooth();
			return;
		}
		temp = []
		var that = this
		if (!that.data.searchingstatus) {
			var that = this
			wx.startBluetoothDevicesDiscovery({
				//指定services搜索 0000ffe0-0000-1000-8000-00805f9b34fb 过滤掉其他类型蓝牙设备
				services: ['0000ffe0-0000-1000-8000-00805f9b34fb'],
				success: function (res) {
					that.setData({
						searchingstatus: !that.data.searchingstatus
					})
				},
				fail: function (res) {
					wx.showToast({
						icon: "none",
						title: that.replaceMsg(res.errCode,res.errMsg),
					})
				}
			})
		} else {
			that.setData({
				searchingstatus: false
			})
			wx.stopBluetoothDevicesDiscovery({
				success: function (res) {
					console.log("停止蓝牙搜索",res)
				}
			})
		}
	},
	connectDevices(e){
		let index = e.target.dataset.index;
		let devices =  this.data.devices[index];
		this.connectTO(devices)
	},	
	connectTO: function (e) {
		var that = this;
		var devices = this.data.devices;
		var index = devices.findIndex(function(item) {
			return item.deviceId === e.deviceId;
		});
		console.log(index)
		if (devices[index].deviceconnected) {
			wx.notifyBLECharacteristicValueChanged({
				state: false, // 停用notify 功能
				deviceId: that.data.connectedDeviceId,
				serviceId: serviceId,
				characteristicId: characteristicId,
				success: function (res) {
					console.log("停用notify 功能")
				}
			})
			wx.closeBLEConnection({
				deviceId: e.deviceId,
				complete: function (res) {
					console.log("断开设备",res);
					devices[index].deviceconnected = false;
					that.setData({
						devices:devices,
						connectedDeviceId: ""
					})
				}
			})
			getApp().globalData.devices = [];
		} else {
			wx.showLoading({
				title: '连接蓝牙设备中...',
			})
			wx.createBLEConnection({
				deviceId: e.deviceId,
				success: function (res) {
					wx.getBLEDeviceServices({
						// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 
						deviceId: e.deviceId,
						success: function (res) {
							//下面两行，Android测试不行的时候注释测试下，Android比较复杂。
							//苹果端可用 安卓端不可用，如果安卓读取失败，可以注释掉这行试试
							wx.getSystemInfo({
								success(resP) {
									if( resP.platform == 'ios'){
										serviceId = res.services[res.services.length - 1].uuid; 
									}else{
										serviceId = res.services[res.services.length - 3].uuid; 
									}
									
								}
							})
							setTimeout(function () {
								wx.getBLEDeviceCharacteristics({
									deviceId: e.deviceId,
									serviceId: serviceId,
									success: function (res) {
										characteristicId = res.characteristics[0].uuid;
										// console.log("设备charater获取成功", res)
										wx.notifyBLECharacteristicValueChanged({
											state: true, // 启用 notify 功能
											deviceId: that.data.connectedDeviceId,
											serviceId: serviceId,
											characteristicId: characteristicId,
											success: function (res) {
												console.log("启用notify")
											},
											fail: function (res) {
												console.log("启用notify失败", res)
											},
										})
									},
									fail: function (res) {
										console.log("设备charater获取失败", res)
									},
								})
							}, 0)
						}
					})
					wx.hideLoading()
					wx.showToast({
						title: '连接成功',
						icon: 'success',
						duration: 1000
					})
					console.log("连接设备成功",res)
					devices.forEach((item) => {
						item.deviceconnected = false
					})
					devices[index].deviceconnected = true;
					that.setData({
						connectedDeviceId: e.deviceId,
						deviceconnected_index: index,
						devices:devices
					})
					getApp().globalData.devices = [e];
					getApp().globalData.connectedDeviceId = e.deviceId;
					wx.setStorageSync('devices', getApp().globalData.devices);
				},
				fail: function (res) {
					wx.hideLoading()
					wx.showToast({
						title: that.replaceMsg(res.errCode,res.errMsg),
						icon: 'none',
						duration: 1000
					})
					console.log("连接设备失败",res)
					that.setData({
						connected: false
					})
				}
			})
			wx.stopBluetoothDevicesDiscovery({
				success: function (res) {
					console.log("停止蓝牙搜索")
				}
			})
		}
	},
	readIdcard(){
		let that = this;
		wx.showLoading({
			title: '读取中~',
		})
		startSearchCard(that)
	},
	replaceMsg(errCode,errMsg){
		let codeMsg = errMsg;
		if(errCode == -1){
			codeMsg = '已连接';
		}else if(errCode == 10001){
			codeMsg = '请检查手机是否已打开蓝牙';
		}else if(errCode == 10000){
			codeMsg = '未初始化蓝牙适配器';
		}else if(errCode == 10002){
			codeMsg = '没有找到指定设备';
		}else if(errCode == 10003){
			codeMsg = '连接失败';
		}else if(errCode == 10006){
			codeMsg = '当前连接已断开';
		}else if(errCode == 10012){
			codeMsg = '连接超时';
		}
		return codeMsg
	}	
})

//寻卡
function startSearchCard(that) {
	searchCardResult = false
	selectCardResult = false
	index = 0
	wx.writeBLECharacteristicValue({
		deviceId: that.data.connectedDeviceId,
		serviceId: serviceId,
		characteristicId: characteristicId,
		value: util.stringToArrayBuffer('aaaaaa96690003200122'),
		success: function (res) {
			console.log(res)
			console.log('writeBLECharacteristicValue success 1', res.errMsg)
		},
		fail: function (res) {
			wx.hideLoading();
			console.log("writeBLECharacteristicValue fail", res)
			wx.showToast({
				title: that.replaceMsg(res.errCode,res.errMsg),
				icon:'none'
			})
		},
	})
}
//寻卡结果分析
function searchCardIsOk(result) {
	var SW1 = result[7];
	var SW2 = result[8];
	var SW3 = result[9];

	if ((0x0 != SW1) || (0x0 != SW2) || (0x9F) != SW3) {
		return false;
	}

	return true;
}
//选卡结果分析
function selectCardIsOk(result) {
	console.log('选卡解析:', result)
	var SW1 = result[7];
	var SW2 = result[8];
	var SW3 = result[9];

	if ((0x0 != SW1) || (0x0 != SW2) || (0x90) != SW3) {
		return false;
	}
	console.log('选卡成功')
	return true;
}
//读卡结果分析
function readCardIsOk(_i_bys_resp) {
	var SW1 = _i_bys_resp[7];
	var SW2 = _i_bys_resp[8];
	var SW3 = _i_bys_resp[9];

	if ((0x0 != SW1) || (0x0 != SW2) || ((0x90) != SW3)) {
		return false;
	}

	if (_i_bys_resp.length < 1024) {
		return false;
	}

	///////////////////////////////////////////////////
	//
	var i = 0;
	var j = 0;
	var iOffset = 0;

	var bysName = new Array(30);
	var bysSexCode = new Array(2);
	var bysNationCode = new Array(4);
	var bysBirth = new Array(16);
	var bysAddr = new Array(70);
	var bysIdCode = new Array(36);
	var bysIssue = new Array(30);
	var bysBeginDate = new Array(16);
	var bysEndDate = new Array(16);

	var iTextSize = 0;
	var iPhotoSize = 0;
	var iFingerSize = 0;

	iTextSize = _i_bys_resp[10] << 8 + _i_bys_resp[11];
	iPhotoSize = _i_bys_resp[12] << 8 + _i_bys_resp[13];
	iFingerSize = _i_bys_resp[14] << 8 + _i_bys_resp[15];

	///////////////////////////////////////////////////
	//截取数据

	//iOffset = 16;
	iOffset = 14;

	//截取姓名
	j = 0;
	for (i = iOffset; i < (iOffset + 30); i++) {
		bysName[j] = _i_bys_resp[i];
		j++;
	}
	try {
		strName = util.uint8ArrayToStr(bysName)
	} catch (Exception) {}
	iOffset += 30;

	//截取性别
	j = 0;
	for (i = iOffset; i < (iOffset + 2); i++) {
		bysSexCode[j] = _i_bys_resp[i];
		j++;
	}
	var strSexCode = null;
	try {
		strSexCode = util.uint8ArrayToStr(bysSexCode);
		strSex = util.getSexFromCode(strSexCode);
	} catch (Exception) {}
	iOffset += 2;

	//截取民族
	j = 0;
	for (i = iOffset; i < (iOffset + 4); i++) {
		bysNationCode[j] = _i_bys_resp[i];
		j++;
	}
	var strNationCode = null;
	try {
		strNationCode = util.uint8ArrayToStr(bysNationCode, );
		strNation = util.getNationFromCode(strNationCode);
	} catch (Exception) {}
	iOffset += 4;

	//截取生日
	j = 0;
	for (i = iOffset; i < (iOffset + 16); i++) {
		bysBirth[j] = _i_bys_resp[i];
		j++;
	}
	try {
		strBirth = new String(bysBirth, "UTF-16LE");
	} catch (Exception) {}
	iOffset += 16;

	//截取地址
	j = 0;
	for (i = iOffset; i < (iOffset + 70); i++) {
		bysAddr[j] = _i_bys_resp[i];
		j++;
	}
	try {
		strAddr = util.uint8ArrayToStr(bysAddr);
	} catch (Exception) {}
	iOffset += 70;

	//截取身份证号
	j = 0;
	for (i = iOffset; i < (iOffset + 36); i++) {
		bysIdCode[j] = _i_bys_resp[i];
		j++;
	}
	try {
		strIdCode = util.uint8ArrayToStr(bysIdCode);		
	} catch (Exception) {}
	iOffset += 36;

	//截取签发机关
	j = 0;
	for (i = iOffset; i < (iOffset + 30); i++) {
		bysIssue[j] = _i_bys_resp[i];
		j++;
	}
	try {
		strIssue = util.uint8ArrayToStr(bysIssue);
	} catch (Exception) {}
	iOffset += 30;

	//截取有效期开始日期
	j = 0;
	for (i = iOffset; i < (iOffset + 16); i++) {
		bysBeginDate[j] = _i_bys_resp[i];
		j++;
	}
	try {
		strBeginDate = util.uint8ArrayToStr(bysBeginDate);
	} catch (Exception) {}
	iOffset += 16;

	//截取有效期结束日期
	j = 0;
	for (i = iOffset; i < (iOffset + 16); i++) {
		bysEndDate[j] = _i_bys_resp[i];
		j++;
	}
	try {
		if (bysEndDate[0] >= '0' && bysEndDate[0] <= '9') {
			// strEndDate = new String(bysEndDate, "UTF-16LE");
		} else {
			// strEndDate = new String(bysEndDate, "UTF-16LE");
		}
		strEndDate = util.uint8ArrayToStr(bysEndDate)
	} catch (Exception) {}
	iOffset += 16;

	// //照片
	// var wlt = new byte[1024];
	// byte[] bmp = new byte[14 + 40 + 308 * 126];
	// for (i = 0; i < iPhotoSize; i++) {
	//   wlt[i] = _i_bys_resp[16 + iTextSize + i];
	// }
	// //int iResult = JniCall.hxgc_Wlt2Bmp(wlt, bmp, 0);
	// int iResult = DecodeWlt.hxgc_Wlt2Bmp(wlt, bmp, 708);
	// Bitmap bitmapIdPhoto = null;
	// if (iResult != -1) {
	//   bitmapIdPhoto = BitmapFactory.decodeByteArray(bmp, 0, bmp.length);
	// }
	return true;

}

function updateInfo(that) {
	console.log('姓名:', strName)
	console.log('性别:', strSex)
	console.log('身份证号:', strIdCode)
	console.log('地址:', strAddr)
	that.setData({
		idCardName: strName,
		idCardNo: strIdCode,
		idCardSex: strSex,
		onreceiving: true
	})
}